package com.czmini.cztest.util;

import org.springframework.cglib.beans.BeanCopier;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.util
 * @ClassName: CopyUtil
 * @Author: 还是那个橙子
 * @Description: 复制工具类
 * @Date: 2022/4/16 17:50
 * @Version: 1.0
 */

public class CopyUtil {
    private static final Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>();

    /**
     * 使用Cglib的BeanCopier实现对象的拷贝
     *
     * @param source
     * @param target
     */
    public static void copyPropertiesCglib(Object source, Object target) {
        String beanKey = generateBeanKey(source.getClass(), target.getClass());
        BeanCopier copier = null;
        if (!beanCopierMap.containsKey(beanKey)) {
            copier = BeanCopier.create(source.getClass(), target.getClass(), false);
            beanCopierMap.put(beanKey, copier);
        } else {
            copier = beanCopierMap.get(beanKey);
        }
        copier.copy(source, target, null);
    }

    public static String generateBeanKey(Class<?> source, Class<?> target) {
        return source.getName() + "@" + target.getName();
    }


}
